// 使用PM2启动服务器
const express = require('express');
const result = require('./models/result');
const database = require('./util/database');
const app = express();

const index = require('./routes/index');
const authentication = require('./routes/authentication');

app.use("/", index);
app.use("/api", authentication);

app.use(function (req, res, next) {
    result.fail(res, 404, "path invalidate for " + req.path);
});
app.use(function (err, req, res, next) {
    result.fail(res, 500, err.message);
});

// 服务器启动时创建所有需要的数据表
database.setup();
const server = app.listen(3000, function () {
    const host = server.address().address;
    const port = server.address().port;
    console.log("Server listening at http://%s:%s", host, port);
});